/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.PaginationUtil;
import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.BuildingTypeListingForm;
import com.ntb.entities.BuildingTypes;
import com.ntb.models.BuildingTypesDAO;
import com.ntb.models.impl.BuildingTypesDAOImpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class BuildingTypeListingAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        BuildingTypeListingForm buildingTypeListingForm = (BuildingTypeListingForm) form;

        BuildingTypesDAO buildingTypesDAO = new BuildingTypesDAOImpl();

        String action = buildingTypeListingForm.getAction();

        List<String> errorMessages = new ArrayList();
        List<String> successMessages = new ArrayList();
        boolean isHaveAction = true;
        if (("delete").equalsIgnoreCase(action)) {
            if (buildingTypesDAO.delete(buildingTypeListingForm.getBuildingTypeID()) == 0) {
                errorMessages.add("There was an error deleting this building type. You must delete all buildings associated with this building type before deleting the building type.");
            } else {
                buildingTypesDAO.delete(buildingTypeListingForm.getBuildingTypeID());
                successMessages.add("1 building types out of 1 successfully deleted.");
            }
        } else if (("deleteBuildingTypes").equalsIgnoreCase(action)) {
            int[] buildingTypes = buildingTypeListingForm.getBuildingTypeIDs();
            int count = 0;

            int length = 0;

            try {
                length = buildingTypes.length;
            } catch (Exception ex) {
            }

            if (length > 0) {
                for (int buildingType : buildingTypes) {
                    if (buildingTypesDAO.delete(buildingType) == 0) {
                        errorMessages.add("There was an error deleting this building type. You must delete all buildings associated with this building type before deleting the building type.");
                    } else {
                        buildingTypesDAO.delete(buildingType);
                        count++;
                    }
                }
                successMessages.add(count + " building types out of " + length + " successfully deleted.");
            }
        } else {
            isHaveAction = false;
        }

        String baseUrl = request.getRequestURI() + "?";

        String typeName = null;
        int page = 1;

        typeName = buildingTypeListingForm.getName();
        typeName = "".equals(typeName) ? null : typeName;
        if (typeName != null) {
            baseUrl += "name=" + typeName;
        }

        try {
            int i = Integer.parseInt(request.getParameter("page"));
            page = i > 1 ? i : 1;
        } catch (Exception ex) {
        }

        Iterator<BuildingTypes> buildingTypeList = buildingTypesDAO.getList(typeName, page);

        if (isHaveAction) {
            request.setAttribute("errorMessages", errorMessages);
            request.setAttribute("successMessages", successMessages);
        }

        request.setAttribute("buildingTypeList", buildingTypeList);

        PaginationUtil paginationUtil = new PaginationUtil();
        paginationUtil.setBaseUrl(baseUrl);
        paginationUtil.setNumPages(buildingTypesDAO.getPageCount(typeName));
        paginationUtil.setCurPage(page);
        request.setAttribute("pagination", paginationUtil.createLinks());

        return mapping.getInputForward();
    }
}
